<template>
  <AmBaseTable ref="AmBaseTableRef"
               :schema="modelSchema"
               :record-config="record"
               @paramsChange="getList"
               :operations="operations"
               :actions="[
                  {
                    label: (row) => {
                      if(row.status === 'recorded') {
                        return '编辑';
                      }
                      return '著录';
                    },
                    auth: ['am:description:edit'],
                    clickCB: this.handleUpdate,
                    tooltip: (row) => {
                      if(row.userId !== currUserId) {
                        return '无权修改他人创建的案卷'
                      } else {
                        return false
                      }
                    },
                    disabledCtrl: (row) => row.userId !== currUserId,
                    buttonProps: {
                      icon: 'el-icon-edit',
                    }
                  },
                  {
                    label: '删除',
                    auth: ['am:description:remove'],
                    tooltip: (row) => {
                      if(row.userId !== currUserId) {
                        return '无权删除他人创建的案卷'
                      } else {
                        return false
                      }
                    },
                    disabledCtrl: (row) => row.userId !== currUserId,
                    clickCB: this.handleDelete,
                    buttonProps: {
                      icon: 'el-icon-delete',
                    }
                  },
                ]">
        <template v-slot:tabs>
          <el-tabs v-model="tabs.activeName" @tab-click="handleTabsClick">
            <el-tab-pane label="已归档卷宗" name="archived_file" />
            <el-tab-pane label="待归档卷宗" name="await_archived_file" />
          </el-tabs>
        </template>
        <template v-slot:column-brStatusName="scope">
          <el-tag :type="brStatusMap[scope.row.brStatus]">{{ scope.row.brStatusName }}</el-tag>
        </template>
        <template v-slot:column-extendedIndex="scope">
          <el-tag v-for="item in scope.row.extendedIndex.split(',')">{{ item }}</el-tag>
        </template>
  </AmBaseTable>
</template>

<script>
import {delDescription, listDescription, apiListDescription} from "@/api/am/description";
import AmBaseTable from "../../../components/AmComp/AmBaseTable.vue";
import {modelSchema} from "../amDescription/model";

export default {
  name: "Description",
  components: {AmBaseTable},
  data() {
    return {
      modelSchema,
      tabs: {
        activeName: 'archived_file'
      },
      record: {
        // 显示搜索条件
        showSearch: true,
        data: [],
        total: 0,
        loading: false,
      },
      brStatusMap: {
        '0': 'success',
        '1': 'warning',
      }
    };
  },
  computed: {
    currUserId() {
      return this.$store.getters.userId
    },
    operations() {
      const operations = [
        {
          label: '导出',
          auth: ['am:description:export'],
          clickCB: this.handleExport,
          buttonProps: {
            type: 'warning',
            icon: 'el-icon-download',
          }
        },
      ]
      if(this.tabs.activeName === 'archived_file') {
        operations.unshift({
          label: '新增',
          auth: ['am:description:add'],
          buttonProps: {
            type: 'primary',
            icon: 'el-icon-plus',
          },
          clickCB: this.handleAdd
        })
      }
      return operations
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getList()
      // this.getApiListDescription()
    });
  },
  methods: {
    // getApiListDescription() {
    //   apiListDescription()
    // },
    /** TAB切换 **/
    handleTabsClick() {
      this.getList();
    },
    /** 查询档案著录列表 */
    getList() {
      this.record.loading = true;
      const queryParams = this.$refs.AmBaseTableRef.getQueryParams();
      queryParams['archivedType'] = this.tabs.activeName;
      listDescription(queryParams).then(response => {
        this.record.data = response.rows;
        this.record.total = response.total;
        this.record.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path: '/am/amDescription/form/add/'})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({path: `/am/amDescription/form/${row.id}`})
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除档案著录编号为"' + ids + '"的数据项？').then(function() {
        return delDescription(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('am/description/export', {
        ...this.queryParams
      }, `description_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
